#define SETTINGS_PLAYER1_NAME 0
#define SETTINGS_PLAYER2_NAME 1
#define SETTINGS_WINDOW_WIDTH 2
#define SETTINGS_WINDOW_HEIGHT 3
#define SETTINGS_WINDOW_FULLSCREEN 4

char settingsArray[5][30];

void createSettingsFile(const char filename[100]) {
	printf("Creating %s\n",filename);
	FILE *outfile;
	outfile = fopen(filename, "w");
	fprintf(outfile,"player1_name Player1\n");
	fprintf(outfile,"player2_name Player2\n");
	fprintf(outfile,"window_width 800\n");
	fprintf(outfile,"window_height 600\n");
	fprintf(outfile,"window_fullscreen 0\n");
	fclose(outfile);
}

void loadSettings(const char filename[100]) {
	
	strcpy(weaponNamesArray[WEAPON_NONE],"None");
	strcpy(weaponNamesArray[WEAPON_CANNON],"Cannon");
	strcpy(weaponNamesArray[WEAPON_RAILGUN],"Railgun");
	strcpy(weaponNamesArray[WEAPON_MINE],"Mine");
	strcpy(weaponNamesArray[WEAPON_SWIRLBOMB],"Swirl Bomb");
	
	printf("Loading settings:\n");
	
	strcpy(settingsArray[SETTINGS_PLAYER1_NAME],"Player1");
	strcpy(settingsArray[SETTINGS_PLAYER2_NAME],"Player2");
	strcpy(settingsArray[SETTINGS_WINDOW_WIDTH],"800");
	strcpy(settingsArray[SETTINGS_WINDOW_HEIGHT],"600");
	strcpy(settingsArray[SETTINGS_WINDOW_FULLSCREEN],"0");
	
	FILE *infile;
	infile = fopen(filename, "r");
	
	if (infile==NULL) {
		perror(filename);
		fclose(infile);
		createSettingsFile(filename);
		return;
	}
	
	char line[100];
	char var[30];
	int number;
	
	while (fgets(line,100,infile)!=NULL)
	{
		//printf("line=%s\n",line);
		if (sscanf( line, "player1_name %s",var)) {
			printf("player1_name = %s",var);
			if (strlen(var) == 0) {
				printf("..setting name to Player1\n");
				strcpy(settingsArray[SETTINGS_PLAYER1_NAME],"Player1");
			} else {
				printf("..setting name to %s\n",var);
				strcpy(settingsArray[SETTINGS_PLAYER1_NAME],var);
			}

		}
		if (sscanf( line, "player2_name %s",var)) {
			printf("player2_name = %s",var);
			if (strlen(var) == 0) {
				printf("..setting name to Player2\n");
				strcpy(settingsArray[SETTINGS_PLAYER2_NAME],"Player2");
			} else {
				printf("..setting name to %s\n",var);
				strcpy(settingsArray[SETTINGS_PLAYER2_NAME],var);
			}
			
		}
		if (sscanf( line, "window_width %i",&number)) {
			if (number < 640 || number > 1920) {
				strcpy(settingsArray[SETTINGS_WINDOW_WIDTH],"640");
			} else {
				sprintf(var,"%i",number);
				strcpy(settingsArray[SETTINGS_WINDOW_WIDTH],var);
			}
		}
		if (sscanf( line, "window_height %i",&number)) {
			if (number < 480 || number > 1200) {
				strcpy(settingsArray[SETTINGS_WINDOW_HEIGHT],"480");
			} else {
				sprintf(var,"%i",number);
				strcpy(settingsArray[SETTINGS_WINDOW_HEIGHT],var);
			}
		}
		if (sscanf( line, "window_fullscreen %i",&number)) {
			if (number != 1) {
				strcpy(settingsArray[SETTINGS_WINDOW_FULLSCREEN],"0");
			} else {
				strcpy(settingsArray[SETTINGS_WINDOW_FULLSCREEN],"1");
			}
		}
	}
	printf("Done!\n\n");
	fclose(infile);
}

void initializeGL() {
	// initiate Microsoft windows extensions
	loadExtensions();
	
	glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
	glEnable(GL_MULTISAMPLE_ARB);			// used with glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	// making default background color black
    glfwSwapInterval(1);
	
	waterShader = createShader(OS_SHADER_PATH(Shader1.vert),OS_SHADER_PATH(shader1.frag));
	stegu1 = createShader(OS_SHADER_PATH(spots.vert),OS_SHADER_PATH(spots.frag));
	stegu2 = createShader(OS_SHADER_PATH(fbmnoise.vert),OS_SHADER_PATH(fbmnoise.frag));
	stegu3 = createShader(OS_SHADER_PATH(flownoise2.vert),OS_SHADER_PATH(flownoise2.frag));
	stegu4 = createShader(OS_SHADER_PATH(snoise3.vert),OS_SHADER_PATH(snoise3.frag));		
	
}